"""
全国疫情可视化地图开发案例
"""
import json
from pyecharts.charts import Map
from pyecharts.options import *
# 读取数据文件
file_name = "D:/python code/1、Python快速入门（8天零基础入门到精通）/第1-12章资料/可视化案例数据/地图数据/疫情.txt"
f = open(file_name,"r",encoding="UTF-8")
data = f.read()
# 将JSON转为python数据
my_dict = json.loads(data)

# 关闭文件
f.close()

# 取到各省数据
children_data = my_dict['areaTree'][0]['children']

# 组装每个省份和确认人数为元组，并将各个省的数据都封装到列表中
data_list = []
for province_data in children_data:
    my_tuple = ()
    province_name = province_data['name']
    if province_name == "北京":
        province_name = province_name+"市"
    elif province_name == "天津":
        province_name = province_name + "市"
    elif province_name == "上海":
        province_name = province_name + "市"
    elif province_name == "重庆":
        province_name = province_name + "市"
    elif province_name == "广西壮族":
        province_name = province_name + "自治区"
    elif province_name == "宁夏回族":
        province_name = province_name + "自治区"
    elif province_name == "新疆维吾尔族":
        province_name = province_name + "自治区"
    elif province_name == "内蒙古":
        province_name = province_name + "自治区"
    elif province_name == "西藏":
        province_name = province_name + "自治区"
    else:
        province_name = province_name+"省"

    province_confirm = province_data['total']['confirm']
    data_list.append((province_name,province_confirm))

# 创建map对象
map = Map()

# 添加数据
map.add("全国疫情确诊人数",data_list,"china")

# 设置全局选项:定制分段的视觉映射
map.set_global_opts(
    title_opts = TitleOpts(title="全国疫情地图"),
    visualmap_opts = VisualMapOpts(
        is_show=True,
        is_piecewise=True, # 是否分段
        pieces=[
            {"min":1,"max":99,"label":"1-99人","color":"#CCFFFF"},
            {"min":100,"max":999,"label":"100-999人","color":"#FFFF99"},
            {"min":1000,"max":4999,"label":"1000-4999人","color":"#FF9966"},
            {"min":5000,"max":9999,"label":"5000-9999人","color":"#FF6666"},
            {"min":10000,"max":99999,"label":"10000-99999人","color":"#CC3333"},
            {"min":100000,"label":"100000+","color":"#990033"}
        ]
    )
)
# 生成地图
map.render("全国疫情地图.html")


